package com.example.jpetstoreclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Animal;
import com.example.jpetstoreclient.domain.CartItem;
import com.example.jpetstoreclient.persistance.CartItemMapper;
import com.example.jpetstoreclient.service.AnimalService;
import com.example.jpetstoreclient.service.CartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cartItemService")
public class CartItemServiceImpl extends ServiceImpl<CartItemMapper, CartItem> implements CartItemService {

    @Autowired
    AnimalService animalService;


    @Override
    public CommonResponse<List<CartItem>> getAllCartItems(int user_id) {
        QueryWrapper<CartItem> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user_id);
        List<CartItem> cartItemList = list(wrapper);

        for (CartItem cartItem : cartItemList){
            int animal_id = cartItem.getAnimal_id();
            Animal animal = animalService.getById(animal_id);
            cartItem.setAnimal(animal);
        }

        return CommonResponse.createForSuccess("查询购物车",cartItemList);
    }

    @Override
    public CommonResponse<Object> addCartItem(CartItem cartItem) {
        int animal_id = cartItem.getAnimal_id();
        int number = cartItem.getNumber();
        Animal animal = animalService.getById(animal_id);
        int stock = animal.getNumber();

        if(number > stock){
            return CommonResponse.createForError("添加购物车失败：库存不足");
        }else {
            if(save(cartItem))return CommonResponse.createForSuccessMessage("添加购物车成功");
            else return CommonResponse.createForError("添加购物车失败");
        }
    }

    @Override
    public CommonResponse<Object> isAvailable(int animal_id, int number) {
        Animal animal = animalService.getById(animal_id);
        int stock = animal.getNumber();
        if(number > stock)return CommonResponse.createForError("库存不足，修改失败！");
        else return CommonResponse.createForSuccessMessage("修改成功！");
    }

    @Override
    public CommonResponse<CartItem> changeQuantity(int cart_id, int number) {
        UpdateWrapper<CartItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cart_id",cart_id).set("number",number);
        if(update(null,updateWrapper)){
            CartItem cartItem = getById(cart_id);
            return CommonResponse.createForSuccess("修改成功！",cartItem);
        }else return CommonResponse.createForError("修改失败！");

    }

    @Override
    public CommonResponse<Object> deleteCartItem(int cart_id) {
        if(removeById(cart_id))return CommonResponse.createForSuccessMessage("删除成功！");
        else return CommonResponse.createForError("删除失败！");
    }

    @Override
    public CommonResponse<Object> deleteSelectedCartItems(List<Integer> ids) {
        if(removeByIds(ids))return CommonResponse.createForSuccessMessage("删除成功！");
        else return CommonResponse.createForError("删除失败！");
    }

    @Override
    public CommonResponse<Object> clearCart(int user_id) {
        QueryWrapper<CartItem> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user_id);
        if(remove(wrapper))return CommonResponse.createForSuccessMessage("清空购物车成功！");
        else return CommonResponse.createForError("清空失败！");
    }


}
